package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGrouponRulesMapper;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    DtsGrouponRulesMapper grouponRulesMapper;


    @Override
    public Map<String, Object> selectList(Integer page, Integer limit, String sort, String order, Integer id) {
        IPage<DtsGrouponRules> iPage = new Page<>(page,limit);

        QueryWrapper wrapper = new QueryWrapper();
        if("desc".equalsIgnoreCase((order))){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }

        if(id!=null){
            wrapper.eq("goods_id",id);
        }
        grouponRulesMapper.selectPage(iPage,wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());

        return map;
    }

    @Override
    public void create(DtsGrouponRules grouponRules) {
        grouponRules.setUpdateTime(new Date());
        grouponRules.setAddTime(new Date());
        grouponRules.setDeleted(false);
        grouponRulesMapper.insert(grouponRules);
    }

    @Override
    public void update(DtsGrouponRules dtsGrouponRules) {

        grouponRulesMapper.updateById(dtsGrouponRules);
    }

    @Override
    public void delete(DtsGrouponRules dtsGrouponRules) {
        grouponRulesMapper.deleteById(dtsGrouponRules.getId());
    }
}
